package karileet.meeting.profile;

import java.util.Calendar;
import java.util.HashMap;
import java.util.Set;

import android.content.Context;
import android.content.SharedPreferences;

/**
 * Class contains methods for retrieving/saving app states from/to shared 
 * preferences
 * 
 * @author markus karileet
 */
public class ProfileConf {
	public static final String PREFS_NAME = "MeetingProfilePrefsFile";
	public static String APP = "Meeting profile";
	public static String PROFILE = "profile";
	public static String VIBRATE = "vibrate";
	public static String START = "start";
	public static String STOP = "stop";
	public static String STARTENABLED = "startenabled";
	public static String STOPENABLED = "stopenabled";
	public static String BEEP = "beep";
	public static String STOPBEG = "beggar";
	public static String DAILYREMINDER = "beggarDaily";
	public static String WEEKDAYS = "weekdays";
	public static String DISABLE = "Disable";
	public static String TOGGLE = "Toggle";
	private Context ctx;
	
	/**
	 * Constructor inits object and populates values from shared preferences
	 * (when existing)
	 * @param ctx
	 */
	public ProfileConf(Context ctx) {
		this.ctx = ctx;
	}
	
	/**
	 * Method returns weekdays that profile should be enabled (every day by default)
	 * @return String weekdays as numbers separated by ;
	 */
	public String getWeekdays() {
		SharedPreferences p = ctx.getSharedPreferences(PREFS_NAME, 0);
		return p.getString(WEEKDAYS, "1;2;3;4;5;6;7");
	}
	
	/**
	 * Method stores weekdays to preferences like 1;3;4;5;6;7
	 * @param weekdays
	 */
	public void setWeekdays(String weekdays) {
		editPreferences(WEEKDAYS, weekdays);
	}
	
	/**
	 * @return int Volume of beep
	 */
	public int getBeepVolume() {
		return getIntPreference(BEEP, 15);
	}

	/**
	 * Method stores user defined beep volume
	 * @param int beepVolume Volume of beep
	 */
	public void setBeepVolume(int beepVolume) {
		editPreferences(BEEP, beepVolume);
	}

	/**
	 * @return boolean whether app is enabled or not
	 */
	public boolean isProfileEnabled() {
		return getBoolPreference(PROFILE);
	}
	
	/**
	 * Method stores profile state to shared preferences
	 * @param boolean profileEnabled Whether app is enabled or not
	 */
	public void setProfileEnabled(boolean profileEnabled) {
    	editPreferences(PROFILE, profileEnabled);
	}
	
	/**
	 * @return boolean whether vibrate is enabled or not
	 */
	public boolean isVibrateEnabled() {
		return getBoolPreference(VIBRATE);
	}
	
	/**
	 * Method stores vibrate state to shared preferences
	 * @param boolean vibrateEnabled Whether vibrate is enabled or not
	 */
	public void setVibrateEnabled(boolean vibrateEnabled) {
		editPreferences(VIBRATE, vibrateEnabled);
	}	
	
	/**
	 * Method checks whether application is allowed to beg money from user
	 * @return boolean
	 */
	public boolean isStopBegEnabled() {
		return getBoolPreference(STOPBEG);
	}
	
	/**
	 * Method sets beg state to true or false
	 * @return boolean
	 */
	public void setStopBeg(boolean val) {
		editPreferences(STOPBEG, val);
	}
	
	public long getDailyReminder() {
		SharedPreferences p = ctx.getSharedPreferences(PREFS_NAME, 0);
		return p.getLong(DAILYREMINDER, 0);
	}
	
	/**
	 * Method updates dailyreminder when 24 hours has passed. When update is 
	 * nessessary, true is returned. Otherwise returns false.
	 * @return
	 */
	public boolean setDailyReminder() {
		long oneDay = 86400000;
		boolean retval = false;
		Calendar AlarmCal = Calendar.getInstance();
		AlarmCal.setTimeInMillis(getDailyReminder());

		//adjust start time, so that service wont start to past time
		if ((AlarmCal.getTimeInMillis() + oneDay) < System.currentTimeMillis()) {
			SharedPreferences p = ctx.getSharedPreferences(PREFS_NAME, 0);
			p.edit().putLong(DAILYREMINDER, System.currentTimeMillis()).commit();
			retval = true;
		}
		return retval;
	}
	
	/**
	 * Method commits state under type to sharedPreferences
	 * @param String type Preference name
	 * @param boolean state State to save
	 */
	private void editPreferences(String type, boolean state) {
		SharedPreferences p = ctx.getSharedPreferences(PREFS_NAME, 0);
		p.edit().putBoolean(type, state).commit();
	}
	
	/**
	 * Method commits state under type to sharedPreferences
	 * @param String type Preference name
	 * @param String state State to save
	 */
	private void editPreferences(String type, String value) {
		SharedPreferences p = ctx.getSharedPreferences(PREFS_NAME, 0);
		p.edit().putString(type, value).commit();
	}
	
	/**
	 * Method commits state under type to sharedPreferences
	 * @param String type Preference name
	 * @param boolean state State to save
	 */
	private void editPreferences(String type, int value) {
		SharedPreferences p = ctx.getSharedPreferences(PREFS_NAME, 0);
		p.edit().putInt(type, value).commit();
	}
	
	private int getIntPreference(String type, int defaultValue) {
		SharedPreferences p = ctx.getSharedPreferences(PREFS_NAME, 0);
		return p.getInt(type, defaultValue);
	}
	
	private boolean getBoolPreference(String type) {
		SharedPreferences p = ctx.getSharedPreferences(PREFS_NAME, 0);
		return p.getBoolean(type, false);
	}
	
	public void setScheduleStopEnabled(boolean stopEnabled) {
		editPreferences(STOPENABLED, stopEnabled);
	}
	public void setScheduleStart(long start) {
		SharedPreferences p = ctx.getSharedPreferences(PREFS_NAME, 0);
		p.edit().putLong(START, start).commit();
	}
	public void setScheduleStop(long stop) {
		SharedPreferences p = ctx.getSharedPreferences(PREFS_NAME, 0);
		p.edit().putLong(STOP, stop).commit();
	}
	public long getScheduleStart() {
		SharedPreferences p = ctx.getSharedPreferences(PREFS_NAME, 0);
		return p.getLong(START, 0);
	}
	public long getScheduleStop() {
		SharedPreferences p = ctx.getSharedPreferences(PREFS_NAME, 0);
		return p.getLong(STOP, 0);
	}
	
	
	public void setScheduleStartEnabled(boolean startEnabled) {
		editPreferences(STARTENABLED, startEnabled);
	}
	
	public boolean isScheduleStartEnabled() {
		return getBoolPreference(STARTENABLED);
	}
	
	public boolean isScheduleStopEnabled() {
		return getBoolPreference(STOPENABLED);
	}
}